package com.wx.activitys.admin.controller;

import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.wx.activitys.base.utils.WebUtil;
import com.wx.activitys.drp.model.BPlatfrom;
import com.wx.activitys.drp.model.BProfit;
import com.wx.activitys.drp.service.BProfitService;
import com.wx.activitys.drp.service.PlatfromService;

@Controller
@RequestMapping("/profit")
public class ProfitController {

	@Autowired
	private BProfitService bProfitService;
	
	@Autowired
	private PlatfromService platfromService;

	/**
	 * 认证审核列表
	 * @param model
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping(value = "list", method = RequestMethod.GET)
	public String list(Model model, HttpServletRequest request) throws UnsupportedEncodingException {
		// String pathRoot =
		// request.getSession().getServletContext().getRealPath("");
		getList(request, model);
		return "admin/profitManage/list";
	}

	private void getList(HttpServletRequest request, Model model){
		int page_index = WebUtil.getInt(request, "pageNo", 1);
		int pageStartRow = (page_index - 1) * 10 ;// 每页的起始数
		BPlatfrom entity = new BPlatfrom();
		entity.getMap().put("pageIndex", pageStartRow);
		entity.getMap().put("pageSize", 10);
		List<BPlatfrom> platfromList = platfromService.queryProfitsPageList(entity);
		int page_all = platfromService.selectCount(entity);
		model.addAttribute("List", platfromList);
		model.addAttribute("totalSize", page_all);//总页数
		model.addAttribute("pageNo", WebUtil.getInt(request, "pageNo", 1));//当前显示页
	}

	
	@RequestMapping(value = "delete", method = RequestMethod.GET)
	public String delete(Model model, HttpServletRequest request,String paltformId) {
		BProfit entity = new BProfit();
		entity.setPaltformId(paltformId);
		entity.setState(0);
		bProfitService.delete(entity);
		getList(request,model);
		return "admin/profitManage/list";
	}
	
	@RequestMapping(value = "add", method = RequestMethod.GET)
	public String add(Model model, HttpServletRequest request) {
		BPlatfrom entity = new BPlatfrom();
		entity.setState(1);
		List<BPlatfrom> platfromList = platfromService.queryALL(entity);
		model.addAttribute("platfromList", platfromList);
		return "admin/profitManage/form";
	}
	

	@RequestMapping(value = "update", method = RequestMethod.GET)
	public String update(Model model, HttpServletRequest request,String paltformId) {
		
		BPlatfrom entity = new BPlatfrom();
		entity.setState(1);
		List<BPlatfrom> platfromList = platfromService.queryALL(entity);
		model.addAttribute("platfromList", platfromList);
		
		BProfit profit = new BProfit();
		profit.setPaltformId(paltformId);
		List<BProfit> profitLiat = bProfitService.queryALL(profit);
		model.addAttribute("profitLiat", profitLiat);
		model.addAttribute("paltformId", paltformId);
		model.addAttribute("type", profitLiat.get(0).getType());
		
		for (BProfit bProfit : profitLiat) {
			if(bProfit.getRoleKey().equals("1")){
				model.addAttribute("value1", bProfit.getValue());
			}
			if(bProfit.getRoleKey().equals("2")){
				model.addAttribute("value2", bProfit.getValue());
			}
			if(bProfit.getRoleKey().equals("3")){
				model.addAttribute("value3", bProfit.getValue());
			}
			if(bProfit.getRoleKey().equals("4")){
				model.addAttribute("value4", bProfit.getValue());
			}
			if(bProfit.getRoleKey().equals("5")){
				model.addAttribute("value5", bProfit.getValue());
			}
		}
		return "admin/profitManage/form";
	}
	
	@RequestMapping(value = "save", method = RequestMethod.POST)
	public String save(Model model, HttpServletRequest request)  {
		
		String paltformId = WebUtil.getString(request, "paltformId", "");
		//String paltformId_ = WebUtil.getString(request, "paltformId_", "");
		String type = WebUtil.getString(request, "type", "");
		String value1 = WebUtil.getString(request, "value1", "");
		String value2 = WebUtil.getString(request, "value2", "");
		String value3 = WebUtil.getString(request, "value3", "");
		String value4 = WebUtil.getString(request, "value4", "");
		String value5 = WebUtil.getString(request, "value5", "");
		int result;
		if(!paltformId.equals("")){
			BProfit profit = new BProfit();
			profit.setPaltformId(paltformId);
			result = bProfitService.delete(profit);
		}
		BProfit entity = new BProfit();
		entity.setType(type);
		entity.setPaltformId(paltformId);
		if(!value1.equals("")){
			entity.setRoleKey("1");
			entity.setValue(Double.valueOf(value1));
			bProfitService.insert(entity);
		}
		if(!value2.equals("")){
			entity.setRoleKey("2");
			entity.setValue(Double.valueOf(value2));
			bProfitService.insert(entity);
			
		}
		if(!value3.equals("")){
			entity.setRoleKey("3");
			entity.setValue(Double.valueOf(value3));
			bProfitService.insert(entity);
		}
		if(!value4.equals("")){
			entity.setRoleKey("4");
			entity.setValue(Double.valueOf(value4));
			bProfitService.insert(entity);
		}
		if(!value5.equals("")){
			entity.setRoleKey("5");
			entity.setValue(Double.valueOf(value5));
			bProfitService.insert(entity);
		}
		
		getList(request,model);
		return "admin/profitManage/list";
	}
}
